/* clipboard.vala
 *
 * Copyright (C) 2010  Richard Talbot-Watkins
 * Copyright (C) 2010  Dave Jeffery
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Authors:
 *  Richard Talbot-Watkins <richtw1@gmail.com>
 * 	Dave Jeffery <david.richard.jeffery@gmail.com>
 */

/**
 * Clipboard.
 *
 * Stores the current selection.
 *
 * @author Richard Talbot-Watkins <richtw1@gmail.com>
 * @author Dave Jeffery <david.richard.jeffery@gmail.com>
 * @version 2010.1227
 * @since 0.1
 */
private class MonsterDesigner.Clipboard : GLib.Object {
	public int width {get; set;}
	public int height {get; set;}
	public uint8[] contents {get; set;}

	/**
	 * Constructor for the Clipboard class.
	 * 
	 * The clipboard data is stored as a stream of bytes, with each byte 
	 * holding the MODE5 logical colour (from 0x0 to 0x3) of one pixel. The
	 * pixels are stored left to right top to bottom. This means width and
	 * height properties are needed to store the shape of the selection.
	 *
	 * A palette has to be applied to convert these colour values into RGB
	 * values for display.
	 *
	 * @param width The width of the area on the clipboard.
	 * @param height The height of the area on the clipboard.
	 * @param contents Contents stored array of indexed bytes (no palette).
	 */
	public Clipboard (int width, int height, uint8[] contents) {
		this.width = width;
		this.height = height;
		this.contents = contents;
	}

	/**
	 * Gets the Cairo RGB value of an indexed byte pixel using a supplied
	 * palette, or null if the coordinates of the pixel are out of range.
	 *
	 * @param point The x and y co-ordinates of the pixel to return.
	 * @param palette_data The palette used to render the indexed byte data.
	 * @return Cairo rgb value or null if the point is out of range.
	 */
	public CairoColour? get_pixel (Point point, PaletteData palette_data) {
		if (point.x < 0 || 
			point.y < 0 || 
			point.x >= width || 
			point.y >= height) { return null; }
		var byte = contents[point.x + point.y * width];
		return palette_data.logical_to_cairo ((int) byte);
	}
}
